package vg.modules.attribute_panel.components;

import java.util.Collection;

import javax.swing.JCheckBox;
import javax.swing.table.AbstractTableModel;

import vg.modules.attribute_panel.components.data.AttributeRow;
import vg.modules.attribute_panel.components.data.AttributeShower;

/**
 * This class realizes model for attribute table
 * @author tzolotuhin
 */
public class AttributeTableModel extends AbstractTableModel {
	private static final long serialVersionUID = 1000L;
	//-------------------------------------------------------------------------
	private String[] columnNames = null;
    private Object[][] table = null;
    private JCheckBox selectAll = null;
    private final static int DEF_COUNT_COLUMN = 4;
	//-------------------------------------------------------------------------
    public AttributeTableModel(final Collection<AttributeRow> data, final JCheckBox selectAll) {
    	this.selectAll = selectAll;
    	this.columnNames = new String[DEF_COUNT_COLUMN];
    	int countRow = 0;
    	if(data != null) {
    		countRow = data.size();
    	}
    	this.table = new Object[countRow][DEF_COUNT_COLUMN];
    	
		int i = 0;
		if(data != null) {
			for(AttributeRow buf : data) {
				this.table[i][0] = buf.getShower();
				this.table[i][1] = buf.getName();
				this.table[i][2] = buf.getValue();
				this.table[i][3] = buf.getType();
				i++;
			}
		}
       	this.columnNames = new String[]{"Show", "Atribute name", "Value atribute", "V/E"};
    }
    public int getColumnCount() {   	
    	return(DEF_COUNT_COLUMN);
    }
    public int getRowCount() {
    	return(this.table.length);
    }
    public Object getValueAt(int rowIndex, int columnIndex) {
    	return(this.table[rowIndex][columnIndex]);
    }
    public String getColumnName(int column) {
    	return(this.columnNames[column]);
    }
    public Class<?> getColumnClass(int columnIndex) {
    	if (getRowCount() > 0)
    		return(this.table[0][columnIndex].getClass());
    	return null;
    }
    public boolean isCellEditable(int rowIndex, int columnIndex) {
    	if(columnIndex == 0 || columnIndex == 2) {
    		return(true);
    	}
    	return(false);
    }
    /**
     * This method sets value in table.
     * @param value - value.
     * @param row   - number of row.
     * @param col   - number of column.
     */
    public void setValueAt(Object value, int row, int col) {
		this.table[row][col] = value;
		//checking
    	if(col == 0) {
    		boolean check = true;
    		for(int i = 0 ; i < this.table.length ; i++) {
    			AttributeShower shower = (AttributeShower)this.table[i][0]; 
    			if(shower.getShow() == AttributeShower.DEF_UNSHOW_GREEN || shower.getShow() == AttributeShower.DEF_UNSHOW_YELLOW) {
    				check = false;
    				break;
    			}
    		}
    		this.selectAll.setSelected(check);
    		this.selectAll.updateUI();
    	}
        fireTableCellUpdated(row, col);
    }  
}